// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/column_selector.h>
#include <pollux/dwio/dwrf/common/common.h>
#include <pollux/dwio/dwrf/reader/stripe_reader_base.h>
#include <pollux/dwio/dwrf/reader/stripe_stream.h>

namespace kumo::pollux::dwrf::detail {

class BinaryStripeStreams {
 public:
  BinaryStripeStreams(
      StripeReaderBase& stripeReader,
      const dwio::common::ColumnSelector& selector,
      uint32_t stripeIndex);

  std::vector<proto::ColumnEncoding> getEncodings(uint32_t nodeId) const;

  std::vector<DwrfStreamIdentifier> getStreamIdentifiers(uint32_t nodeId) const;

  std::unique_ptr<proto::RowIndex> getRowGroupIndex(
      const EncodingKey ek,
      std::string_view label) const {
    return ProtoUtils::readProto<proto::RowIndex>(stripeStreams_.getStream(
        stripeStreams_.format() == DwrfFormat::kDwrf
            ? ek.forKind(proto::Stream_Kind_ROW_INDEX)
            : ek.forKind(proto::orc::Stream_Kind_ROW_INDEX),
        label,
        false));
  }

  std::unique_ptr<dwio::common::SeekableInputStream> getStream(
      const DwrfStreamIdentifier& si,
      std::string_view label) const {
    return stripeStreams_.getCompressedStream(si, label);
  }

  uint64_t getStreamLength(const DwrfStreamIdentifier& si) const {
    return stripeStreams_.getStreamLength(si);
  }

  const StripeInformationWrapper& getStripeInfo() const {
    return stripeReadState_->stripeMetadata->stripeInfo;
  }

 private:
  bool preload_;
  std::shared_ptr<StripeReadState> stripeReadState_;
  dwio::common::RowReaderOptions options_;
  StripeStreamsImpl stripeStreams_;
  melon::F14FastMap<uint32_t, std::vector<uint32_t>> encodingKeys_;
  melon::F14FastMap<uint32_t, std::vector<DwrfStreamIdentifier>>
      nodeToStreamIdMap_;
};

class BinaryStreamReader {
 public:
  BinaryStreamReader(
      const std::shared_ptr<ReaderBase>& reader,
      const std::vector<uint64_t>& columnIds);

  uint32_t getStrideLen() const;

  std::unique_ptr<BinaryStripeStreams> next();

  melon::F14FastMap<uint32_t, proto::ColumnStatistics> getStatistics() const;

  uint32_t getCurrentStripeIndex() const {
    return stripeIndex_;
  }

  uint32_t numStripes() const {
    return numStripes_;
  }

 private:
  const dwio::common::ColumnSelector columnSelector_;
  const uint32_t numStripes_;
  StripeReaderBase stripeReaderBase_;
  uint32_t stripeIndex_;
};

} // namespace kumo::pollux::dwrf::detail
